/**
 * 
 * 
 * 
 */
package utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * The Class Datum provides a date element to the general application It has 3 constructors: 1.
 * without parameter (use of system date) 2. with parameter: 1. int dag, int maand, int jaar 2.
 * string format: DDMMYYYY - input format will be D/MM/YYYY
 * 
 * @author Nicolas Vanderhulst
 * @version 1.0
 * @serial 2011091801
 */
public class Datum {
	private int dag;
	private int maand;
	private int jaar;

	public Datum() {

		try {
			Calendar cal = Calendar.getInstance();
			cal.setLenient(false);
			maand = cal.get(Calendar.MONTH);
			dag = cal.get(Calendar.DAY_OF_MONTH);
			jaar = cal.get(Calendar.YEAR);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Datum(int day, int month, int year) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setLenient(false);
			cal.set(Calendar.MONTH - 1, month);
			cal.set(Calendar.DAY_OF_MONTH, day);
			cal.set(Calendar.YEAR, year);
			maand = cal.get(Calendar.MONTH);
			dag = cal.get(Calendar.DAY_OF_MONTH);
			jaar = cal.get(Calendar.YEAR);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Datum(String date) {
		String[] tmp;
		String delimiter = "/";
		tmp = date.split(delimiter);

		for (int i = 0; i < tmp.length; i++) {
			try {
				Calendar cal = Calendar.getInstance();
				cal.setLenient(false);
				cal.set(Calendar.MONTH - 1, Integer.parseInt(tmp[1]));
				cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(tmp[0]));
				cal.set(Calendar.YEAR, Integer.parseInt(tmp[2]));
				maand = cal.get(Calendar.MONTH);
				dag = cal.get(Calendar.DAY_OF_MONTH);
				jaar = cal.get(Calendar.YEAR);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @return This method returns the Date in US format
	 */
	public String getDatumInAmerikaansFormaat() {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, maand);
		cal.set(Calendar.YEAR, jaar);
		cal.set(Calendar.DAY_OF_MONTH, dag);
		Date g = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
		String sDate = sdf.format(g);
		return sDate;

	}

	/**
	 * @return This method returns the Date in EU format
	 */
	public String getDatumInEuropeesFormaat() {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, maand);
		cal.set(Calendar.YEAR, jaar);
		cal.set(Calendar.DAY_OF_MONTH, dag);
		Date g = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String sDate = sdf.format(g);
		return sDate;
	}

	/**
	 * @return This method returns the Date with the month in String format
	 */
	@Override
	public String toString() {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, maand);
		cal.set(Calendar.YEAR, jaar);
		cal.set(Calendar.DAY_OF_MONTH, dag);
		Date g = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("d MMMM yyyy");
		String sDate = sdf.format(g);
		return sDate;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + dag;
		result = prime * result + jaar;
		result = prime * result + maand;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Datum)) {
			return false;
		}
		Datum other = (Datum) obj;
		if (dag != other.dag) {
			return false;
		}
		if (jaar != other.jaar) {
			return false;
		}
		if (maand != other.maand) {
			return false;
		}
		return true;
	}

	/**
	 * @return This method compares two dates
	 */
	public int compareTo(Object o) {
		Datum d = (Datum) o;
		if (d.jaar < jaar) {
			return -1;
		} else if (d.jaar > jaar) {
			return 1;
		} else {
			if (d.maand < maand) {
				return -1;
			} else if (d.maand > maand) {
				return 1;
			} else {
				if (d.dag < dag) {
					return -1;
				} else if (d.dag > dag) {
					return 1;
				} else {
					return 0;
				}
			}
		}
	}

	/**
	 * @return This method checks if the given date is smaller than the reference date
	 */
	public boolean kleinerDan(Datum d) {
		if (d.jaar < jaar || d.maand < maand || d.dag < dag) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @return This method returns the difference in years
	 */
	public int verschilInJaren(Datum d) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, d.maand);
		cal.set(Calendar.YEAR, d.jaar);
		cal.set(Calendar.DAY_OF_MONTH, d.dag);
		// Date g = cal.getTime();

		Calendar cal1 = Calendar.getInstance();
		cal1.setLenient(false);
		cal1.set(Calendar.MONTH, maand);
		cal1.set(Calendar.YEAR, jaar);
		cal1.set(Calendar.DAY_OF_MONTH, dag);
		long c1 = cal.getTimeInMillis();
		long c2 = cal1.getTimeInMillis();
		long diff = c2 - c1;
		long diffDay = diff / (24 * 60 * 60 * 1000);
		long diffYear = (long) (diffDay / 365.2425);
		int n = (int) diffYear;

		if (n < 0) {
			n = -n;
		}
		return n;

	}

	/**
	 * @return This method returns the difference in months
	 */
	public int verschilInMaanden(Datum d) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, d.maand);
		cal.set(Calendar.YEAR, d.jaar);
		cal.set(Calendar.DAY_OF_MONTH, d.dag);
		// Date g = cal.getTime();

		Calendar cal1 = Calendar.getInstance();
		cal1.setLenient(false);
		cal1.set(Calendar.MONTH, maand);
		cal1.set(Calendar.YEAR, jaar);
		cal1.set(Calendar.DAY_OF_MONTH, dag);
		int n = (cal.get(Calendar.YEAR) - cal1.get(Calendar.YEAR)) * 12
				+ (cal.get(Calendar.MONTH) - cal1.get(Calendar.MONTH))
				+ (cal.get(Calendar.DAY_OF_MONTH) >= cal1.get(Calendar.DAY_OF_MONTH) ? 0 : -1);

		if (n < 0) {
			n = -n;
		}
		return n;

	}

	/**
	 * @return This method returns the difference in days
	 */
	public int verschilInDagen(Datum d) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, d.maand);
		cal.set(Calendar.YEAR, d.jaar);
		cal.set(Calendar.DAY_OF_MONTH, d.dag);

		Calendar cal1 = Calendar.getInstance();
		cal1.setLenient(false);
		cal1.set(Calendar.MONTH, maand);
		cal1.set(Calendar.YEAR, jaar);
		cal1.set(Calendar.DAY_OF_MONTH, dag);
		long c1 = cal.getTimeInMillis();
		long c2 = cal1.getTimeInMillis();
		long diff = c2 - c1;
		long diffDay = diff / (24 * 60 * 60 * 1000);

		int n = (int) diffDay;
		if (n < 0) {
			n = -n;
		}
		return n;
	}

}